#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 10;
int a[N], dq[N];

void solve() {
	int n, k; cin >> n >> k;
	for (int i = 1; i <= n; i++) cin >> a[i];
	int hh = 1, tt = 0;
	for (int i = 1; i <= n; i++) {
		while (hh <= tt && dq[hh] <= i - k) hh++;
		while (hh <= tt && a[dq[tt]] <= a[i]) tt--;
		dq[++tt] = i;
		if (i >= k) cout << a[dq[hh]] << " ";
	}
	cout << '\n';
	hh = 1, tt = 0;
	for (int i = 1; i <= n; i++) {
		while (hh <= tt && dq[hh] <= i - k) hh++;
		while (hh <= tt && a[dq[tt]] >= a[i]) tt--;
		dq[++tt] = i;
		if (i >= k) cout << a[dq[hh]] << " ";
	}
	cout << '\n';
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	solve();
	return 0;
}


